package top.wilsonlv.jaguar.cloud.upms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import top.wilsonlv.jaguar.basecrud.BaseController;
import top.wilsonlv.jaguar.cloud.upms.entity.Device;
import top.wilsonlv.jaguar.cloud.upms.mapper.DeviceMapper;
import top.wilsonlv.jaguar.cloud.upms.sdk.vo.DeviceVO;
import top.wilsonlv.jaguar.cloud.upms.service.DeviceService;
import top.wilsonlv.jaguar.commons.mybatisplus.extension.JaguarLambdaQueryWrapper;
import top.wilsonlv.jaguar.commons.web.response.JsonResult;

/**
 * <p>
 * 终端设备  前端控制器
 * </p>
 *
 * @author lvws
 * @since 2022-01-13
 */
@Validated
@RestController
@RequestMapping("/device")
@Api(tags = "终端设备管理")
public class DeviceController extends BaseController<Device, DeviceMapper, DeviceService> {

    @ApiOperation(value = "分页查询终端设备")
    @PreAuthorize("hasAuthority('终端设备管理')")
    @GetMapping(value = "/page")
    public JsonResult<Page<DeviceVO>> page(@ApiIgnore Page<Device> page,
                                           @ApiParam("名称") @RequestParam(required = false) String deviceName,
                                           @ApiParam("UID") @RequestParam(required = false) String deviceUid,
                                           @ApiParam("类型") @RequestParam(required = false) String deviceType,
                                           @ApiParam("型号") @RequestParam(required = false) String deviceModel,
                                           @ApiParam("系统版本") @RequestParam(required = false) String deviceSysVersion,
                                           @ApiParam("客户端版本") @RequestParam(required = false) String deviceClientVersion,
                                           @ApiParam("IP") @RequestParam(required = false) String deviceIp) {
        LambdaQueryWrapper<Device> wrapper = JaguarLambdaQueryWrapper.<Device>newInstance()
                .eq(Device::getDeviceName, deviceName)
                .eq(Device::getDeviceUid, deviceUid)
                .eq(Device::getDeviceType, deviceType)
                .eq(Device::getDeviceModel, deviceModel)
                .eq(Device::getDeviceSysVersion, deviceSysVersion)
                .eq(Device::getDeviceClientVersion, deviceClientVersion)
                .eq(Device::getDeviceIp, deviceIp);
        return success(service.queryDevice(page, wrapper));
    }

    @ApiOperation(value = "终端设备详情")
    @PreAuthorize("hasAuthority('终端设备管理')")
    @GetMapping(value = "/{id}")
    public JsonResult<DeviceVO> detail(@PathVariable Long id) {
        return success(service.getDetail(id));
    }

}